﻿using Common.BaseLibrary.Authorization;
using DepartmentWork.Migrator.Data;
using DepartmentWork.Model;
using DepartmentWork.Platform.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DepartmentWork.Platform.Areas.QueryArea.Controllers
{
    public class ProjectWorkSelcetController : BaseController
    {
        //
        // GET: /QueryArea/ProjectWorkSelcet/

        public ActionResult SelcetProjectWork()
        {
 


            //#region 获取该团队空间下的全部人员
            //Guid teamSpaceId = userTeamsapceRepository.GetModel(r => r.USER_ID == new Guid(CurrentUser.UserID)).FirstOrDefault().TEAMSPACE_ID;
            //List<UserInfoViewModel> userList = new List<UserInfoViewModel>();
            //List<USER_TEAMSAPCE> userTeams = userTeamsapceRepository.GetModel(r => r.TEAMSPACE_ID == teamSpaceId).ToList();
            //userList.AddRange((from item in userTeams
            //                   select new UserInfoViewModel()
            //                   {
            //                       UserId = item.USER_ID
            //                   }).ToList());
            //for (int i = 0; i < userList.Count(); i++)
            //{
            //    var id = userList[i].UserId;
            //    userList[i].UserName =
            //        userRepository.GetModel(r => r.USER_ID == id).FirstOrDefault().DISPLAY_NAME;
            //}
            //TempData["UserList"] = userList;
            //#endregion

        
            return View();
        }







        /// <summary>
        /// 项目工作查询AJAX
        /// </summary>
        /// <param name="NameN">项目名称关键字</param>
        /// <param name="NameId">负责人关键字</param>
        /// <param name="BTime">开始时间</param>
        /// <param name="DTime">结束时间</param>
        /// <returns></returns>
        public JsonResult SelcetS(string NameN, string NameId, DateTime BTime, DateTime DTime)
        {

            //r.DESKWORK_DEADINE.Subtract(DateTime.Now).Days < 0
            string str = NameN.Trim();
            string name = NameId.Trim();
            List<ProjectManagementInfoViewModel> list = new List<ProjectManagementInfoViewModel>();
            List<PROJECT_MANAGEMENT> model = new List<PROJECT_MANAGEMENT>();
            if (str != "")//如果有关键字
            {
                #region 全部负责人
                if (name == "") //负责人：全部 
                {

                    model = projectManagementRepository.GetModel(r => r.PROJECT_MANAGEMENT_TITLE.Contains(NameN) && r.PROJECT_MANAGEMENT_CREATETIME >= BTime && r.PROJECT_MANAGEMENT_CREATETIME <= DTime).ToList();

                }
                #endregion
                #region  选择了指定的负责人
                else //选择了指定的负责人
                {
                    model = projectManagementRepository.GetModel(r => r.PROJECT_MANAGEMENT_TITLE.Contains(NameN) && r.PROJECT_MANAGEMENT_LEADER.Contains(NameId) && r.PROJECT_MANAGEMENT_CREATETIME >= BTime && r.PROJECT_MANAGEMENT_CREATETIME <= DTime).ToList(); ;//所有符合关键字和时间的 项目

                }
                #endregion
            }
            else // 如果没有关键字
            {
                #region 全部负责人
                if (name == "") //负责人：全部 
                {
                    model = projectManagementRepository.GetModel(r => r.PROJECT_MANAGEMENT_CREATETIME >= BTime && r.PROJECT_MANAGEMENT_CREATETIME <= DTime).ToList();
                }
                #endregion
                #region  选择了指定的负责人
                else //选择了指定的负责人
                {
                    model = projectManagementRepository.GetModel(r => r.PROJECT_MANAGEMENT_CREATETIME >= BTime && r.PROJECT_MANAGEMENT_CREATETIME <= DTime && r.PROJECT_MANAGEMENT_LEADER.Contains(NameId)).ToList();

                }
                #endregion
            }


            #region
            if (model.Count > 0)
            {
                foreach (PROJECT_MANAGEMENT x in model.OrderByDescending(p=>p.PROJECT_MANAGEMENT_CREATETIME))
                {

                    ProjectManagementInfoViewModel pl = new ProjectManagementInfoViewModel();
                    pl.PROJECT_MANAGEMENT_BUINESS_CONTACT = x.PROJECT_MANAGEMENT_BUINESS_CONTACT;
                    pl.PROJECT_MANAGEMENT_BUINESS_DEPARTMENT = x.PROJECT_MANAGEMENT_BUINESS_DEPARTMENT;
                    pl.PROJECT_MANAGEMENT_BUINESS_TEL = x.PROJECT_MANAGEMENT_BUINESS_TEL;
                    pl.PROJECT_MANAGEMENT_BUINESS_TYPE = x.PROJECT_MANAGEMENT_BUINESS_TYPE;
                    pl.PROJECT_MANAGEMENT_CONTENT = x.PROJECT_MANAGEMENT_CONTENT;
                    pl.PROJECT_MANAGEMENT_CREATETIME = x.PROJECT_MANAGEMENT_CREATETIME;
                    pl.PROJECT_MANAGEMENT_CREATETIME_SELCET = x.PROJECT_MANAGEMENT_CREATETIME.ToString("yyyy-MM-dd");
                    pl.PROJECT_MANAGEMENT_DEADTIME = x.PROJECT_MANAGEMENT_DEADTIME;
                    pl.PROJECT_MANAGEMENT_DEADTIME_SELCET = x.PROJECT_MANAGEMENT_DEADTIME.ToString("yyyy-MM-dd");
                    pl.PROJECT_MANAGEMENT_ID = x.PROJECT_MANAGEMENT_ID;
                    pl.PROJECT_MANAGEMENT_LEADER = x.PROJECT_MANAGEMENT_LEADER;
                    pl.PROJECT_MANAGEMENT_NO = x.PROJECT_MANAGEMENT_NO;
                    pl.PROJECT_MANAGEMENT_STATUS = x.PROJECT_MANAGEMENT_STATUS;
                 
                    if (x.PROJECT_MANAGEMENT_STATUS == 1)
                    {
                        pl.PROJECT_MANAGEMENT_STATUS_SElCET = "结束";
                    }
                    else
                    {
                        pl.PROJECT_MANAGEMENT_STATUS_SElCET = "进行中";
                    }
                    pl.PROJECT_MANAGEMENT_TITLE = x.PROJECT_MANAGEMENT_TITLE;
                    pl.TEAMSPACE_ID = x.TEAMSPACE_ID;
                    
                    list.Add(pl);
                }
            }
            #endregion
            return Json(list);
        }


    }
}
